package com.woniuxy.kaer.system.controller;


import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.system.annotation.RequirePermission;
import com.woniuxy.kaer.system.annotation.RequireRole;
import com.woniuxy.kaer.system.dto.RoleResourceDTO;
import com.woniuxy.kaer.system.model.Resources;
import com.woniuxy.kaer.system.service.RoleResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sun
 * @since 2025年02月17日
 */
@RestController
@RequestMapping("/roleResource")
@Api(tags = "角色资源管理")
public class RoleResourceController {
    @Resource
    private RoleResourceService roleResourceService;

    //新增角色资源
    @RequireRole("BOSS")
    @ApiOperation(value = "新增角色资源")
    @PostMapping("/addRoleResource")
    @RequirePermission("/roleResource/addRoleResource")
    public Result addRoleResource(Integer roleId, Integer resourceId) throws Exception {
        roleResourceService.addRoleResource(roleId, resourceId);
        return Result.succeed("新增角色资源成功",null);
    }
    //减少角色资源
    @RequireRole("BOSS")
    @ApiOperation(value = "减少角色资源")
    @DeleteMapping("/deleteRoleResource")
    @RequirePermission("/roleResource/deleteRoleResource")
    public Result deleteRoleResource(Integer roleId, Integer resourceId) throws Exception {
        roleResourceService.removeRoleResource(roleId, resourceId);
        return Result.succeed("删除角色资源成功",null);
    }

    //修改角色资源
    @RequireRole("BOSS")
    @PostMapping("/updateRoleResource/{roleId}")
    @ApiOperation(value = "修改角色资源")
    @RequirePermission("分配角色资源")
    public Result updateRoleResource(@RequestBody RoleResourceDTO roleResourceDTO) throws Exception {
         roleResourceService.updateRoleResource(roleResourceDTO);
         return Result.succeed("修改角色资源成功",null);
    }

    //查看该角色已有的权限资源
    @GetMapping("/getRoleResource/{roleId}")
    @ApiOperation("通过角色ID查询权限")
    @RequireRole("BOSS")
    @RequirePermission("角色ID查询权限")
    public Result getRoleResource(@PathVariable Integer roleId) throws Exception {
        System.out.println("roleId="+roleId);
        final List<Resources> roleResource = roleResourceService.getRoleResource(roleId);
        return Result.succeed("获取该角色已有的权限成功",roleResource);
    }


}

